package com.xunji.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunji.standard.entity.TXjNdFlPersonInformation;
import com.xunji.standard.mapper.TXjNdFlPersonInformationMapper;
import com.xunji.standard.service.TXjNdFlPersonInformationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【t_xj_nd_fl_person_information】的数据库操作Service实现
 * @createDate 2025-08-18 14:38:40
 */
@Service
public class TXjNdFlPersonInformationServiceImpl extends ServiceImpl<TXjNdFlPersonInformationMapper, TXjNdFlPersonInformation>
        implements TXjNdFlPersonInformationService {

    @Override
    public List<TXjNdFlPersonInformation> selectListByCaseId(String caseId, String keyword) {
        QueryWrapper<TXjNdFlPersonInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        if (keyword != null && !keyword.isEmpty()) {
            qw.and(wrapper -> wrapper.like("xm", keyword).or().like("sfzh", keyword));
        }
        return this.baseMapper.selectList(qw);
    }

    @Override
    public List<TXjNdFlPersonInformation> selectListByCaseIdAndInfoId(String caseId, Long infoId, String xm, String sfzh) {
        QueryWrapper<TXjNdFlPersonInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        qw.eq(!StringUtils.isEmpty(xm), "xm", xm).eq(!StringUtils.isEmpty(sfzh), "sfzh", sfzh);
        return this.baseMapper.selectList(qw);
    }

    @Override
    public List<TXjNdFlPersonInformation> selectListByCaseIdAndXmAndSfzh(String caseId, String xm, String sfzh) {
        QueryWrapper<TXjNdFlPersonInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).eq("xm", xm).eq("sfzh", sfzh);
        return this.baseMapper.selectList(qw);
    }

    @Override
    public void deleteByCaseId(String caseId, String xm, String sfzh) {
        // 删除caseId 下的 xm 或 sfzh 的人员信息
        QueryWrapper<TXjNdFlPersonInformation> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        qw.eq(!StringUtils.isEmpty(xm), "xm", xm);
        qw.eq(!StringUtils.isEmpty(sfzh), "sfzh", sfzh);
        this.baseMapper.delete(qw);
    }

    @Override
    public List<String> getlxfsList(String caseId, Long infoId, String xm, String sfzh) {
        ArrayList<String> lsfsList = new ArrayList<>();
        List<TXjNdFlPersonInformation> list = selectListByCaseIdAndInfoId(caseId, infoId, xm, sfzh);
        if (list != null && !list.isEmpty()) {
            for (TXjNdFlPersonInformation pi : list) {
                String lsfs = pi.getLxfs();
                if (!StringUtils.isEmpty(lsfs)) {
                    String[] split = lsfs.split(",");
                    lsfsList.addAll(Arrays.asList(split));
                }
            }
        }
        // 去重lxhm
        Set<String> lxhmSet = new HashSet<>();
        for (String lxhm : lsfsList) {
            if (StringUtils.isEmpty(lxhm)) continue;
            lxhmSet.add(lxhm);
        }
        return new ArrayList<>(lxhmSet);
    }

    @Override
    public void updateOwnerInfo(String caseId, TXjNdFlPersonInformation personInformation, TXjNdFlPersonInformation ownerInformation, String commonModifyContent) {
        // 处理原归属人手机号
        String lxfs = personInformation.getLxfs();
        if (lxfs != null) {
            String str = lxfs.replaceAll(commonModifyContent, ""); // 去除手机号
            personInformation.setLxfs(removeExtraCommas(str));
            this.updateById(personInformation);
        }

        // 处理新归属人手机号
        lxfs = ownerInformation.getLxfs();
        if (lxfs != null) {
            // 末尾增加手机号
            String str = lxfs + "," + commonModifyContent;
            ownerInformation.setLxfs(removeExtraCommas(str));
            this.updateById(ownerInformation);
        }
    }


    /**
     * 去除字符串首尾的英文逗号，并将中间连续的多个英文逗号替换为单个逗号
     *
     * @param input 输入字符串
     * @return 处理后的字符串
     */
    public static String removeExtraCommas(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        // 1. 去除首尾的英文逗号
        String trimmed = input.trim();
        if (trimmed.startsWith(",")) {
            trimmed = trimmed.substring(1);
        }
        if (trimmed.endsWith(",")) {
            trimmed = trimmed.substring(0, trimmed.length() - 1);
        }

        // 2. 去除中间重复的英文逗号，只保留一个
        return trimmed.replaceAll(",+", ",").trim();
    }
}
